/**
 * 
 */
package com.mango.mstock;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.mango.common.model.Security;
import com.mango.common.util.CommonUtil;
import com.mango.common.util.StringUtil;
import com.mango.mstock.support.utils.MStockUtil;

/**
 * @author mango
 *
 */
@SuppressLint("HandlerLeak")
public class ListViewAdapter extends BaseAdapter {

	private Context context;
	private List<Map<String, Object>> listItems;
	private LayoutInflater listContainer;

	public final class ListItemView {
		public TextView code;
		public TextView name;
		public TextView latestprice;
		public TextView changepct;
		public Button popupBtn;
	}

	public ListViewAdapter(Context context, final List<Map<String, Object>> listItems) {
		this.context = context;
		this.listItems = listItems;
		listContainer = LayoutInflater.from(context);

		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				notifyDataSetChanged();
			}
		};
		new Thread(new Runnable() {
			@Override
			public void run() {
				// run once to update latest price for each item.
				for (Map<String, Object> item : listItems) {
					String code = String.valueOf(item.get("code"));
					Security favorite = MStockUtil.getLatestMyFavorite(code);
					if (favorite == null) {
						// do not update if no latest price found
						continue;
					}
					item.put("latestprice", favorite.getLatestPrice());
					item.put("changepct", favorite.getChangePct());
				}
				handler.sendMessage(Message.obtain());
			}
		}).start();
	}

	@Override
	public int getCount() {
		return listItems.size();
	}

	@Override
	public Object getItem(int arg0) {
		return listItems.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}
	
	public void addItem(Map<String, Object> item) {
		if (item != null) {
			this.listItems.add(item);
			notifyDataSetChanged();
		}
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.e("method", "getView");
		ListItemView listItemView = null;
		if (convertView == null) {
			listItemView = new ListItemView();
			convertView = listContainer.inflate(R.layout.list_item, null);

			listItemView.code = (TextView) convertView.findViewById(R.id.code);
			listItemView.name = (TextView) convertView.findViewById(R.id.name);
			listItemView.latestprice = (TextView) convertView
					.findViewById(R.id.latestprice);
			listItemView.changepct = (TextView) convertView
					.findViewById(R.id.changepct);
			listItemView.popupBtn = (Button) convertView
					.findViewById(R.id.popupBtn);
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}

		final String code = String.valueOf(listItems.get(position).get("code"));
		listItemView.code.setText(code);
		listItemView.name.setText(String.valueOf(listItems.get(position).get("name")));
		
		float yesterdayprice = (Float) listItems.get(position).get("yesterdayprice");
		float latestprice = (Float) listItems.get(position).get("latestprice");
		
		if (latestprice == 0) {
			// suspending
			listItemView.latestprice.setText("--");
			listItemView.changepct.setText("--");
			listItemView.popupBtn.setText(R.string.suspended);
			listItemView.popupBtn.setEnabled(false);
		} else {
			listItemView.latestprice.setTextColor(MStockUtil.getColorByLatestPrice(yesterdayprice, latestprice));
			listItemView.latestprice.setText(StringUtil.formatFloat(latestprice, 2));
			
			listItemView.changepct.setTextColor(MStockUtil.getColorByLatestPrice(yesterdayprice, latestprice));
			listItemView.changepct.setText(CommonUtil.getChangePct(yesterdayprice, latestprice));
			
			listItemView.popupBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, FloatingService.class);
					intent.putExtra("code", code);
					context.startService(intent);

					if (context instanceof MainActivity) {
						((MainActivity) context).finish();
					}
				}
			});
		}

		return convertView;
	}

}
